#pragma once

#include <iostream>
#include <string>
#include <vector>
#include <ctemplate/template.h>

// #include "oj_model1.hpp"
#include "oj_model2.hpp"

/*************************
 * 实现渲染
 *************************/

namespace ns_view
{
	using namespace std;
	using namespace ns_model;

	const string template_path = "./template_html/";
	class View
	{
	public:
		View() {}
		~View() {}

	public:
		// html是返回给http服务的网页
		// 渲染题目列表页
		void AllExpandHtml(const vector<struct Question> &questions, string *html)
		{
			// 题目的编号 题目的标题 题目的难度
			// 推荐使用表格显示
			// 1.形成路径
			string all_qs_html = template_path + "all_questions.html";
			// 2.形成数字典
			ctemplate::TemplateDictionary root("all_questions");
			for (const auto &q : questions)
			{
				ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("question_list");
				sub->SetValue("number", q.number);
				sub->SetValue("title", q.title);
				sub->SetValue("star", q.star);
			}
			// 3.获取被渲染的html
			ctemplate::Template *tpl = ctemplate::Template::GetTemplate(all_qs_html, ctemplate::DO_NOT_STRIP);

			// 4.开始渲染功能
			tpl->Expand(html, &root);
		}

		// 渲染单个题目的详细页面
		void OneExpandHtml(const struct Question &q, string *html)
		{
			// 1.形成路径
			string one_qs_html = template_path + "one_questions.html";
			// 2.形成数字典
			ctemplate::TemplateDictionary root("one_questions");
			root.SetValue("number", q.number);
			root.SetValue("title", q.title);
			root.SetValue("star", q.star);
			root.SetValue("desc", q.desc);
			root.SetValue("pre_code", q.header);
			// 3.获取被渲染的html
			ctemplate::Template *tpl = ctemplate::Template::GetTemplate(one_qs_html, ctemplate::DO_NOT_STRIP);
			// 4.开始渲染功能
			tpl->Expand(html, &root);
		}
	};
}